package d2_polymorphism;

public class Text {
    public static void main(String[] args) {
        //目标：理解多态的好处
        //好处1：可以实现解耦合，右边对象可以随时切换，后续业务随机改变
       People p1 = new Teacher();
       p1.run();
        //p1.text()  多态下存在的问题：无法直接调用子类的独有功能 //编译看左边，运行看右边
        //可以用强制类型转换俩解决上述问题

        //强制转换
      //  Student s1 = (Student) p1;
       // s1.test();
        //强制类型转换可能存在的问题：编译阶段有继续或者实现关系就可以强制转换，但是运行时可能出现类型转换异常
       //  Teacher  t1 =  (Teacher) p1;
       // t1.run();//  运行时出现 ClassCastException 类型转换异常
        if(p1 instanceof Student){
            Student s2 = (Student) p1;
            s2.test();
        }else if(p1 instanceof Teacher){
            Teacher t2 =  (Teacher) p1;
            t2.teach();
        }

        System.out.println("------------------------");
        //好处2：可以使用父类类型，作为形参，可以接收一切子类对象
        Student s =new Student();
        go(s);
      Teacher t = new Teacher();
        go(t);
    }

    public static void go(People p){
        p.run();
        if(p instanceof Student){
            Student s = (Student) p;
            s.test();
        }else if(p instanceof Teacher){
            Teacher t =  (Teacher) p;
            t.teach();
        }

    }
}